package 
{
	import b2.net.*;
	import b2.player.*;
	import b2.scene.*;
	import b2.scene.base.*;
	import b2.scene.guide.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.warehouse.*;
	import b2.widget.task.*;
	import b2.widget.login.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.utils.*;

	[SWF(backgroundColor="#AAAAAA", frameRate="60", quality="MEDIUM", width="800", height="600")]
	public class B2dev extends Sprite
	{
		public function B2dev()
		{
			//调试面板
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			//读入参数
			var param:Object = loaderInfo.parameters;
			//人人网用户
			if (param.hasOwnProperty("xn_sig_user") && parseInt(param["xn_sig_user"]) > 0)
			{
				//人人用户信息
				PlayerInfoManager.getInstance().myRenRenId = param["xn_sig_user"];
				Profile.rrSessionKey = param["xn_sig_session_key"];
				//服务器session及时间同步
				PlayerInfoManager.getInstance().myUserId = param["userId"];
				ServerHelper.getInstance().sessionId = param["sessionId"];
				ServerHelper.getInstance().serverTime = param["timeStamp"];
				ServerHelper.getInstance().clientTime = getTimer() / 1000;
				//调试信息
				DebugPane.getInstance().addInfo("xn_sig_user:" + param["xn_sig_user"]);
				DebugPane.getInstance().addInfo("xn_sig_session_key:" + param["xn_sig_session_key"]);
				DebugPane.getInstance().addInfo("userId:" + param["userId"]);
				DebugPane.getInstance().addInfo("sessionId:" + param["sessionId"]);
				DebugPane.getInstance().addInfo("serverTime:" + param["timeStamp"]);
				DebugPane.getInstance().addInfo("current seconds:" + ServerHelper.getInstance().currentSeconds());
				//开始进入
				startAll();
			}
			//尚未授权
			else
			{
				//本地调试用
				if (Profile.DEBUG == 1)
				{
					PlayerInfoManager.getInstance().myUserId = 24;
					startAll();
				}
				//远程调试用
				else if (Profile.DEBUG == 2)
				{
//					PlayerInfoManager.getInstance().myRenRenId = 388110645;//佟
//					PlayerInfoManager.getInstance().myUserId = 1;//平
					PlayerInfoManager.getInstance().myUserId = 2;//小雨
					Profile.rrSessionKey = "2.3601bfc618c8bef0e19d6a4cf3eab18c.3600.1326088800-245030154";
					startAll();
				}
				//发布版本（服务器无调试）
				else
				{
					var authenMessage:AuthenMessage = new AuthenMessage();
					authenMessage.x = (BFont.stageWidth - authenMessage.width) / 2;
					authenMessage.y = (BFont.stageHeight - authenMessage.height) / 3;
					addChild(authenMessage);
				}
			}
		}
		
		private function startAll():void
		{
			//SFS服务器连接
			SmartFoxHelper.getInstance().connect();
			//开始加载主资源
			var loadingScene:LoadingScene = new LoadingScene("正在加载主资源……");
			SceneManager.getInstance().loadScene(loadingScene);
			ResourceHelper.getInstance().startLoadMainResource(loadingScene.loadBar.updateProgress, main);
			addChild(SceneManager.getInstance());
		}
		
		public function onKeyDown(e:KeyboardEvent):void 
		{
			if (e.keyCode == 27)
			{
				if (DebugPane.getInstance().parent == null)
				{
					addChild(DebugPane.getInstance());
				}
				else
				{
					DebugPane.getInstance().close();
				}
			}
		}
		
		/**
		 * 加载完成后的主函数
		 */
		public function main():void
		{
			//主音乐
			BSound.getInstance().playMain();
			ServerHelper.getInstance().addEventListener(ServerEvent.LOGIN_ME, onLoginComplete);
			//调试版本
			if (Profile.DEBUG > 0)
			{
				BSound.getInstance().musicVolume = 0;
				BSound.getInstance().effectVolume = 0;
				ServerHelper.getInstance().loginMeDebug();
			}
			//发布版本
			else
			{
				BSound.getInstance().musicVolume = 0.5;
				BSound.getInstance().effectVolume = 0.5;
				ServerHelper.getInstance().loginMe();
			}
		}
		
		private function onLoginComplete(event:ServerEvent):void
		{
			//SFS用户登陆
			SmartFoxHelper.getInstance().login();
			//加载主基地
			SceneManager.getInstance().loadScene(BaseScene.getInstance());
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			Warehouse.getInstance().refresh();
			TaskDialog.getInstance().refresh();
			//加载新手引导
			if (!me.finishGuide)
			{
				var guideScene:GuideScene = GuideScene.getInstance();
				addChild(guideScene);
				guideScene.step = 0;
			}
			else if (!me.hasFetchedGift())
			{
				var loginDialog:LoginDialog = new LoginDialog(me.logonDayCount);
				WidgetManager.getInstance().popOut([loginDialog], true, true);
			}
			//失去焦点时候提示
			var focusLostScreen:FocusLostScreen = FocusLostScreen.getInstance();
			addChild(focusLostScreen);
			stage.addEventListener(Event.DEACTIVATE, onFocusLost);
			stage.addEventListener(Event.ACTIVATE, onFocus);
			focusLostScreen.addEventListener(MouseEvent.CLICK, onFocus);
		}
		
		private function onFocusLost(event:Event):void
		{
			FocusLostScreen.getInstance().enable = true;
		}

		private function onFocus(event:Event):void
		{
			FocusLostScreen.getInstance().enable = false;
		}
	}
}
